const express = require('express');
const request = require('request');
const session = require('express-session')
const router = express.Router();

/* GET signin page. */
router.get('/', function(req, res, next) {
    if(req.session.logged){
        res.redirect('/')
    }
    else{
        res.render('signin')
    }  
});


router.post('/', function(req, res, next) {
    const url = 'http://userlogin:5000/login'
    request({
        url: url,
        method: "POST",
        json: true,
        body: JSON.stringify(req.body)
    }, function(error, response, body){
        if(!error && response.statusCode == 200){
            if(body.status == 1){
                req.session.logged = true;
                req.session.uid = body.uid;
                req.session.name = body.name;
                req.session.studentID = body.studentID;
                req.session.gender = body.gender;
                res.redirect('/')
            }
            else{
                res.send(body.message)
            }
        }
        else
        {
            res.send('Error, try again')
        }

    });
  });

module.exports = router;
